﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TrainTicketBookingSystem
{
    public partial class RefoundForm : Form
    {
        private User u = null;
        UserService userService = new UserService();
        private DataGridViewRow selectedRow = null;
        private List<User> usersList = new List<User>();
        private List<Order> ordersList = new List<Order>();
        private OrderService orderService = new OrderService();
        private StationService stationService = new StationService();
        private CarriageService carriageService = new CarriageService();
        private TrainSequenceService trainSequenceService = new TrainSequenceService();
        public RefoundForm()
        {
            InitializeComponent();
            u = userService.FindUserByAccount(LoginFrom.userAccount);
            showInTable();
        }

        private void showInTable()
        {
            ordersList = orderService.FindOrderByAccount(u.Account);
            dataTable.RowCount = ordersList.Count;
            for (int i = 0; i < ordersList.Count; i++)
            {
                TrainSequence ts = trainSequenceService.FindTrainSequenceById(ordersList[i].TrainSequenceId);
                dataTable[0, i].Value = ts.TrainNum;
                dataTable[1, i].Value = ts.TrainId;
                dataTable[2, i].Value = stationService.FindStationNameById(ts.StartStation);
                dataTable[3, i].Value = stationService.FindStationNameById(ts.EndStation);
                dataTable[4, i].Value = ts.LaunchTime.ToString();
                dataTable[5, i].Value = ts.ArriveTime.ToString();
                dataTable[6, i].Value = ts.TravelTime.ToString();
                dataTable[7, i].Value = ordersList[i].SeatId;
                dataTable[8, i].Value = carriageService.FindPriceBySeatId(ordersList[i].SeatId);
                dataTable[9, i].Value = ordersList[i].OrderId;
            }
        }

        private void btnReturn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void RefoundForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            UserForm.userForm.Show();
        }

        private void btnRefund_Click(object sender, EventArgs e)
        {
            if (textBoxPwd.Text == null || u.Password != textBoxPwd.Text.Trim()|| selectedRow == null)
            {
                MessageBox.Show(textBoxPwd.Text == null ? "请输入密码进行验证" :
                    u.Password != textBoxPwd.Text.Trim() ? "密码错误，请重新验证" : "请选择要退票的订单");
                return;
            }
            RefoundTicket();
            MessageBox.Show("退票成功，已自动返回");
            this.Close();
            return;
        }

        private void RefoundTicket()
        {
            int OrderId = int.Parse(selectedRow.Cells[9].Value.ToString());
            orderService.DeleteOrderById(OrderId);
            orderService.DeleteOrderById(OrderId);
        }

        private void dataGridViewTrainInFo_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
            {
                selectedRow = dataTable.Rows[e.RowIndex];
            }
        }
    }
}
